-- UIRelicDesc
-- Create by cheny3
-- 掠夺说明界面

--UIRelicDesc继承自Layer
UIRelicDesc = class("UIRelicDesc", function()
    return cc.Layer:create();
end);

function UIRelicDesc.create()
    return UIRelicDesc.new();
end

local SCRAP_CLASS_ID = 10000;

--构造函数
function UIRelicDesc:ctor()
    self:setName("UIRelicDesc");

    --初始化/复用飞船说明界面
    local node = cc.CSLoader:createNode("layout/sky_city/AirShipDesc.csb");
    self:addChild(node);
    self.node = node;

    -- 初始数据
    self:initData();

    -- 重绘
    self:redraw();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize();
end

-- 说明信息
function UIRelicDesc:initData()
    -- 额外奖励说明
    local extraBonusDesc = "";
    local extraBonus = SkyRelicM.getRobRelicExtraBonus();
    local winTimes = ME.user.dbase:query("relic_rob_win_times") or 0;
    for needTimes, bonusInfo in pairs(extraBonus) do
        local bonusTip = "";
        -- 每日额外奖励数据
        for index, perBonus in pairs(bonusInfo) do
            bonusTip = spliceLocStr(bonusTip,
                            string.format(getLocStr("item_bonus_format"),
                                          ItemM.query(perBonus[2], "name"), perBonus[3]));
            if index ~= #bonusInfo then
                bonusTip = bonusTip .. getLocStr("pause_mark");
            end
        end

        -- 判断是否达成，区分颜色
        if needTimes > winTimes then
            -- 灰色
            extraBonusDesc = extraBonusDesc .. "[gray]" .. string.format(getLocStr("relic_explain_desc3"),
                needTimes, bonusTip) .. "[-]" .. "\n";
        else
            -- 蓝色
            extraBonusDesc = extraBonusDesc .. "[blue]" .. string.format(getLocStr("relic_explain_desc3"),
                 needTimes, bonusTip) .. "[-]" .. "\n";
        end
    end

    local timeDesc = getLocStr("relic_refresh_time_desc");
    local refreshDesc = string.format(getLocStr("relic_explain_desc4"), timeDesc);
    local detailDesc = getLocStr("relic_explain_desc") ..
        getLocStr("relic_explain_desc2") .. extraBonusDesc .. "\n" .. refreshDesc;

    self.title1 = getLocStr("relic_explain_title");
    self.desc1  = detailDesc;
end

-- 到服务器零点剩余时间
function UIRelicDesc:tomorrowZeroLeftTime()
    -- 当前时间
    local curTime = TimeM.getServerTime();

    -- 计算服务器明天0点的时间戳
    local refreshTime = TimeM.calcZeroTime() + SECONDS_PER_DAY;

    return refreshTime - curTime;
end

-- 倒计时格式
function UIRelicDesc:analysisLeftTime(leftTime)
    if leftTime > 3600 then
        return math.floor(leftTime/3600) .. "h";
    elseif leftTime > 60 then
        return math.floor(leftTime/60) .. "m";
    end
    return math.floor(leftTime) .. "s";
end

-- 重绘
function UIRelicDesc:redraw()
    -- 标题
    local firstTitleLabel = findChildByName(self.node,"CT/combat_title");
    local firstDescLabel = findChildByName(self.node,"CT/combat_desc");
    local lastTitleLabel = findChildByName(self.node,"CT/select_title");
    local lastDescLabel = findChildByName(self.node,"CT/select_desc");

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");

    TextStyleM.setTextStyle(firstTitleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(lastTitleLabel,TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(firstDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    TextStyleM.setTextStyle(lastDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    firstTitleLabel:setString(self.title1);

    local function tick()
        -- 拼接到明天凌晨时间
        local leftTime = self:tomorrowZeroLeftTime();
        local timeStr  = self:analysisLeftTime(leftTime);
        TextStyleM.setTextStyle(firstDescLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        firstDescLabel = applyString(firstDescLabel, string.format(self.desc1, timeStr));
        performWithDelay(self, tick, 1);
    end
    tick();

    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { firstTitleLabel, firstDescLabel}, { AlignM.DEFAULT_GAP, AlignM.DEFAULT_GAP});
end

-- 注册点击事件
function UIRelicDesc:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIRelicDesc");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册点击任意地方关闭当前窗口事件
    local btnBgBack = findChildByName(self.node, "bg/bgImg");
    local function onBgBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIRelicDesc");
        end
    end
end

-- 适配
function UIRelicDesc:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end


